﻿Imports Infragistics.Win
Imports Infragistics.Win.UltraWinEditors
Imports Crystal.CLL
Imports DevExpress.Utils
Imports CrystalClientManagement.BLL
Imports Infragistics.Win.UltraWinGrid.ExcelExport
Imports Infragistics.Documents.Excel
Imports Infragistics.Win.UltraWinGrid
Imports Microsoft.Practices.EnterpriseLibrary.Logging
Imports Infragistics.Documents
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel

Public Class frmClientManagement

#Region "Declare"
    Private _NullField As String = "--------"
    Private _ClientName As String = "Client Name"
    Private _OnLoad As Boolean = False
    Private _SearchField As String = String.Empty
    Private _SearchValue As String = String.Empty
    Private _AllSearch As Boolean = False
    Private _IsSortColumn As Boolean = False
#End Region

#Region "Form Load"

    Private Sub ClientManagement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadFunctionality()
        cmbSearchField.Appearance.ForeColor = Color.Black
    End Sub

#End Region

#Region "Form Closing"

    Private Sub frmClientManagement_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If IsChildFormOpened() = False Then
            e.Cancel = True
        End If
    End Sub

#End Region

#Region "Button Events"

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnExeSummary_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExeSummary.Click
        MessageBox.Show("Under development.", BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        If grdClients Is Nothing Then Exit Sub
        If Not grdClients.Rows.Count > 0 Then Exit Sub
        Dim Logmessage As New System.Text.StringBuilder
        Dim fileName As String = String.Empty
        Dim dialog As New SaveFileDialog
        Dim showDialog As New DialogResult
        Dim name As String
        Dim ExpToExcel As New UltraGridExcelExporter
        Dim listOfHiddenColumns As New List(Of UltraWinGrid.UltraGridColumn)
        Dim listOfHiddenRows As New List(Of UltraGridRow)
        Dim excel As Microsoft.Office.Interop.Excel.Application
        Dim wb As Microsoft.Office.Interop.Excel.Workbook
        Try
            fileName = "Clients - " + DateTime.Now.ToString()
            fileName = fileName.Replace(":", "_").Replace("/", "_")
            With dialog
                .FileName = fileName
                .InitialDirectory = "C:\\"
                .Filter = "Excel (2007-2010 format)(*.xlsx)|*.xlsx|Excel (97-2003 format)(*.xls)|*.xls"
                .DefaultExt = ".xlsx"
            End With
            showDialog = dialog.ShowDialog()
            If showDialog = DialogResult.OK Then
                Dim strExtension As String = System.IO.Path.GetExtension(dialog.FileName)
                name = dialog.FileName
                Dim selectedformat As WorkbookFormat

                If strExtension = ".xlsx" Then
                    selectedformat = WorkbookFormat.Excel2007
                Else
                    selectedformat = WorkbookFormat.Excel97To2003
                End If
                pnlCBContainer.Enabled = False
                pnlButton.Enabled = False

                Dim ExcWorkbook As New Infragistics.Documents.Excel.Workbook(selectedformat, WorkbookPaletteMode.CustomPalette)
                Dim lobjws As Infragistics.Documents.Excel.Worksheet = ExcWorkbook.Worksheets.Add("Client")
                'UltraGridExcelExporter1.Export(grdClients, lobjws, 0, 0)
                If rdSearchResult.Checked Then
                    ExpToExcel.Export(grdClients, lobjws, 0, 0)
                Else

                    Dim row1 As UltraWinGrid.UltraGridRow
                    Dim column As UltraWinGrid.UltraGridColumn
                    For Each column In grdClients.DisplayLayout.Bands(0).Columns
                        If column.Index > 0 Then
                            If column.Hidden = True Then
                                listOfHiddenColumns.Add(column)
                            End If
                            column.Hidden = False
                        End If
                    Next
                    For Each row1 In grdClients.Rows
                        If row1.Hidden = True Then
                            listOfHiddenRows.Add(row1)
                        End If
                        row1.Hidden = False
                    Next
                    ExpToExcel.Export(grdClients, lobjws, 0, 0)
                    If listOfHiddenRows.Count > 0 Then
                        For Each row1 In listOfHiddenRows
                            row1.Hidden = True
                        Next
                    End If
                    If listOfHiddenColumns.Count > 0 Then
                        For Each column In listOfHiddenColumns
                            column.Hidden = True
                        Next
                    End If
                End If
                ExcWorkbook.Save(name)
                lblMessage.Visible = False
                pnlCBContainer.Enabled = True
                pnlButton.Enabled = True
                ShowMessageInfo(MessageManager.GetMessage("EXCELFILECREATED"))
                Try
                    ' Dim Filename As String
                    excel = New Microsoft.Office.Interop.Excel.Application
                    wb = excel.Workbooks.Open(name, , False)
                    excel.Visible = True
                    'wb.Activate()d
                Catch ex As COMException
                    MessageBox.Show("Error accessing Excel: " + ex.ToString())

                Catch ex As Exception
                    MessageBox.Show("Error: " + ex.ToString())
                End Try
            End If

        Catch ex As Exception
            ShowMessageInfo(ex.Message)
            Logmessage.Append("There is error in Export of form Client management.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            pnlButton.Enabled = True
            fileName = String.Empty
            dialog = Nothing
            showDialog = Nothing
            name = Nothing
            ExpToExcel = Nothing
            Logmessage = Nothing
            listOfHiddenColumns = Nothing
            listOfHiddenRows = Nothing
        End Try
    End Sub

    Private Sub btnAddClient_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddClient.Click
        If (CType(sender, Control).Name = "btnAddClient") Then
            For lmFomrs As Integer = 0 To System.Windows.Forms.Application.OpenForms.Count - 1
                If System.Windows.Forms.Application.OpenForms(lmFomrs).Name = "frmAddEditClient" Then
                    If CType(System.Windows.Forms.Application.OpenForms(lmFomrs), frmAddEditClient).WindowState = FormWindowState.Minimized Then
                        CType(System.Windows.Forms.Application.OpenForms(lmFomrs), frmAddEditClient).WindowState = FormWindowState.Maximized
                    Else
                        System.Windows.Forms.Application.OpenForms(lmFomrs).BringToFront()
                    End If
                    If (FocusTheExistingInstanceOfForm("", "frmAddEditClient", CType(System.Windows.Forms.Application.OpenForms(lmFomrs), frmAddEditClient).WindowState)) Then
                        Return
                    End If
                End If
            Next
            Dim addEditClient As New frmAddEditClient
            addEditClient.Text = "Crystal - Add Client"
            addEditClient.txtStreet1.Focus()
            AddHandler addEditClient.AddParticularClientInGrid, AddressOf AddParticularClientInGrid
            addEditClient.Show()
            'Return
        End If

    End Sub
#End Region

#Region "Radio Events"

    Private Sub rdAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdAll.Click
        If rdAll.Checked Then
            _AllSearch = True
            LoadClients()
        End If
    End Sub

    Private Sub rdSearchResult_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdSearchResult.Click
        If txtSearchIn.Text = String.Empty Then
            txtSearchIn.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim
        End If
    End Sub
#End Region

#Region "Methods"

    Private Sub LoadFunctionality()
        Dim Logmessage As New System.Text.StringBuilder
        Try
            Me.Text = TitleManager.GetTitle("ClientManagement")
            txtSearchIn.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT")
            ShowDefaultSelectedColumns()
            DefaultDisableCheckedColumns()
            cmbSearchField.DataSource = LoadSearchField()
            rdAll.Checked = True
            cmbSearchField.Text = _ClientName
            grdClients.ContextMenu = New ContextMenu()
            Me.grdClients.DisplayLayout.Override.CellClickAction = UltraWinGrid.CellClickAction.CellSelect
            DisableButtonEditDelete(False)
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in LoadFunctionality of form ClientManagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Sub

    Public Sub ShowDefaultSelectedColumns()
        Dim Logmessage As New System.Text.StringBuilder
        Try
            chkClientName.Checked = True
            chkCountry.Checked = True
            chkNotes.Checked = True
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in ShowDefaultSelectedColumns of form ClientManagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Sub

    Public Sub DefaultDisableCheckedColumns()
        Dim Logmessage As New System.Text.StringBuilder
        Try
            chkClientName.Enabled = False
            chkClientName.Checked = True
            chkClientName.ForeColor = Color.White
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in ShowDefaultSelectedColumns of form ClientManagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Sub

    Private Function LoadSearchField() As List(Of String)
        Dim Logmessage As New System.Text.StringBuilder
        Dim lField As New List(Of String)

        Try
            For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
                If ctrl.[GetType]().Equals(GetType(UltraCheckEditor)) Then
                    If DirectCast(ctrl, UltraCheckEditor).Checked Then
                        lField.Add(ctrl.Text)
                    End If
                End If
            Next
            lField.Add(_NullField)
            For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
                If (ctrl.[GetType]().Equals(GetType(UltraCheckEditor))) Then
                    If Not DirectCast(ctrl, UltraCheckEditor).Checked Then
                        lField.Add(ctrl.Text)
                    End If
                End If
            Next
            Return lField
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in ClientSearch of form ClientManagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            lField = Nothing
            Logmessage = Nothing
        End Try
    End Function

    Private Sub DisableButtonEditDelete(ByVal bool As Boolean)
        btnEdit.Enabled = bool
        btnDelete.Enabled = bool
        btnExport.Enabled = bool
    End Sub

    Public Sub ShowCheckedGridColumns()
        Dim Logmessage As New System.Text.StringBuilder
        Try
            For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
                If DirectCast(ctrl, UltraCheckEditor).Checked Then
                    With grdClients
                        If .DisplayLayout.Bands(0).Columns.Exists(ctrl.Text) Then
                            .DisplayLayout.Bands(0).Columns(ctrl.Text).Hidden = False
                        End If
                    End With
                Else
                    With grdClients
                        If .DisplayLayout.Bands(0).Columns.Exists(ctrl.Text) Then
                            .DisplayLayout.Bands(0).Columns(ctrl.Text).Hidden = True
                        End If
                    End With
                End If
            Next
            If (grdClients.Rows.Count > 0) Then
                grdClients.DisplayLayout.Bands(0).Columns("Client Name").PerformAutoResize(grdClients.Rows.Count)
            End If
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in ClientSearch of form ClientManagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Sub

    Private Sub SetSearchText()
        If txtSearchIn.Text = String.Empty Then
            txtSearchIn.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim
        End If
    End Sub
    Private Function GetRowCount(ByVal Clients As System.Data.DataTable) As Integer
        Dim Logmessage As New System.Text.StringBuilder

        Dim view As New DataView(Clients)
        Dim distinctValues As System.Data.DataTable = view.ToTable(True, "Client Name")
        Try
            Dim count As Integer = distinctValues.Rows.Count
            Return count
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in ClientSearch of form ClientManagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            view = Nothing
            distinctValues = Nothing
            Logmessage = Nothing

        End Try
    End Function
    Private Sub LoadClients()
        Dim Logmessage As New System.Text.StringBuilder
        Dim Searchin As Integer
        Dim LookFor As String = ""
        Dim Clients As New System.Data.DataTable
        Dim IDR As IDataReader = Nothing
        Dim Criteria As Short
        Dim waitDialog As New WaitDialogForm()
        Dim count As Integer = 0
        Try
            lblMessage.Visible = False
            System.Windows.Forms.Application.DoEvents()
            _SearchField = cmbSearchField.Text
            If rdContains.Checked Then
                Criteria = SearchCriteria.Contains
            Else
                Criteria = SearchCriteria.BeginWith
            End If
            '*************************************************************
            Select Case cmbSearchField.Value.ToLower
                Case "client name"
                    Searchin = ClientManagement.ClientMenu.ClientName
                Case "listing"
                    Searchin = ClientManagement.ClientMenu.Listing
                Case "ratio"
                    Searchin = ClientManagement.ClientMenu.Ratio
                Case "ison"
                    Searchin = ClientManagement.ClientMenu.ISIO
                Case "sedol"
                    Searchin = ClientManagement.ClientMenu.SEDOL
                Case "cusip"
                    Searchin = ClientManagement.ClientMenu.CUSIP
                Case "sector"
                    Searchin = ClientManagement.ClientMenu.Sector
                Case "industry"
                    Searchin = ClientManagement.ClientMenu.Industry
                Case "market cap"
                    Searchin = ClientManagement.ClientMenu.MarketCap
                Case "cap group"
                    Searchin = ClientManagement.ClientMenu.CapGroup
                Case "primary exchange"
                    Searchin = ClientManagement.ClientMenu.PrimaryExchange
                Case "street1"
                    Searchin = ClientManagement.ClientMenu.Street1
                Case "street2"
                    Searchin = ClientManagement.ClientMenu.Street2
                Case "city"
                    Searchin = ClientManagement.ClientMenu.City
                Case "state"
                    Searchin = ClientManagement.ClientMenu.State
                Case "country"
                    Searchin = ClientManagement.ClientMenu.Country
                Case "region"
                    Searchin = ClientManagement.ClientMenu.Region
                Case "mobile"
                    Searchin = ClientManagement.ClientMenu.Mobile
                Case "telephone"
                    Searchin = ClientManagement.ClientMenu.Telephone
                Case "fax"
                    Searchin = ClientManagement.ClientMenu.Fax
                Case "email"
                    Searchin = ClientManagement.ClientMenu.Email
                Case "notes"
                    Searchin = ClientManagement.ClientMenu.Notes
            End Select
            If _AllSearch Then
                rdAll.Checked = True
                LookFor = String.Empty
                _AllSearch = False
            Else
                LookFor = txtSearchIn.Text.Trim
                rdSearchResult.Checked = True
            End If

            If rdAll.Checked And LookFor = String.Empty Then
                Searchin = ClientManagement.ClientMenu.ClientName
            End If
            System.Windows.Forms.Application.DoEvents()
            pnlButton.Enabled = False
            waitDialog.Show()
            IDR = ClientManagement.SearchClients(Searchin, LookFor.Replace("'", "''").Replace("_", "[_]").Replace("%", "[%]"), Criteria)
            Clients.Load(IDR)
            BaseClass.ChangeDataReaderDataTable(Clients)

            If CommonCLL.CheckEmptyDataTable(Clients) Then
                grdClients.DataSource = Nothing
                waitDialog.Close()
                MessageBox.Show(MessageManager.GetMessage("NORECORD"), BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                lblTotalClients.Text = "Total clients:- 0"
                DisableButtonEditDelete(False)
                Exit Sub
            Else
                grdClients.DataSource = Clients
                lblTotalClients.Text = "Total clients:- " & GetRowCount(Clients)
                DefaultDisableCheckedColumns()
                ShowCheckedGridColumns()
                DisableButtonEditDelete(True)
                If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
                    HideRows(True)
                End If
            End If
            HideDefaultColumn()
            'Me.grdClients.DisplayLayout.Bands(0).Columns(1).MergedCellEvaluationType = _
            '    MergedCellEvaluationType.MergeSameText
            Me.grdClients.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
            'Me.grdClients.DisplayLayout.Bands(0).Columns(1).MergedCellEvaluationType = _
            '    MergedCellEvaluationType.MergeSameText
            'Me.grdClients.DisplayLayout.Override.MergedCellStyle = MergedCellStyle.Always

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in LoadClient of form Management.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Clients = Nothing
            IDR = Nothing
            lblTotalClients.Visible = True
            waitDialog.Close()
            Logmessage = Nothing
            pnlButton.Enabled = True
        End Try
    End Sub
    Private Sub HideRows(ByVal IsTrue As Boolean)
        Dim row As UltraGridRow
        For Each row In grdClients.Rows
            'If TypeOf row Is UltraGridGroupByRow Then
            '    Dim groupByRow As UltraGridGroupByRow = DirectCast(row, UltraGridGroupByRow)
            'End If
            'If Not Nothing Is row.ChildBands Then
            '    ' Loop throgh each of the child bands.
            '    Dim childBand As UltraGridChildBand = Nothing
            'For Each childBand In row.ChildBands
            For i As Integer = 0 To grdClients.Rows.Count
                If i < grdClients.Rows.Count - 1 Then
                    If grdClients.Rows(i).Cells IsNot Nothing AndAlso grdClients.Rows(i + 1).Cells IsNot Nothing Then
                        If grdClients.Rows(i).Cells(0).Value.ToString = grdClients.Rows(i + 1).Cells(0).Value.ToString Then
                            grdClients.Rows(i + 1).Hidden = IsTrue
                        End If
                    End If
                End If
            Next
            ' Call this method recursivedly for each child rows collection.
            'Next
            'End If

        Next
    End Sub
    Private Sub HideDefaultColumn()
        grdClients.DisplayLayout.Rows.Band.Columns("ClientID").Hidden = True
    End Sub

    Private Sub MultiRowSelection(ByVal isTrue As Boolean)
        btnEdit.Enabled = isTrue
        btnExeSummary.Enabled = isTrue
        btnDelete.Enabled = isTrue
    End Sub

    Private Sub ShowMessageInfo(ByVal message As String)
        MessageBox.Show(message, "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function IsChildFormOpened() As Boolean
        For lmFomrs As Integer = 0 To System.Windows.Forms.Application.OpenForms.Count - 1
            If System.Windows.Forms.Application.OpenForms(lmFomrs).Name = "frmAddEditClient" Then
                System.Windows.Forms.Application.OpenForms(lmFomrs).BringToFront()
                Return False
            End If
        Next
        Return True
    End Function
    Public Function FocusTheExistingInstanceOfForm(ByVal id As String, ByVal Filename As String, ByVal windowstate As FormWindowState) As Boolean
        Dim objAdd As frmAddEditClient = Nothing
        Dim objClient As frmClientManagement = Nothing
        Try
            For i As Integer = 0 To My.Application.OpenForms.Count - 1
                Select Case Filename

                    Case "frmAddEditClient"
                        If (My.Application.OpenForms(i).Name = Filename) Then
                            objAdd = My.Application.OpenForms(i)
                            If (objAdd.Client = id) Then
                                If (windowstate = FormWindowState.Maximized) Then
                                    objAdd.WindowState = FormWindowState.Maximized
                                End If
                                If (windowstate = FormWindowState.Normal) Then
                                    objAdd.WindowState = FormWindowState.Normal
                                End If
                                objAdd.Focus()
                                Return True
                            End If
                        End If
                End Select
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            ''Logging
            Dim MsgLog As New System.Text.StringBuilder
            MsgLog.Append("There is error in FocusTheExistingInstanceOfForm in Client Management.")
            MsgLog.Append(Environment.NewLine)
            MsgLog.Append("Error Details:- ")
            MsgLog.Append(ex.ToString())
            CreateErrorLog(MsgLog)
            MsgLog = Nothing
        Finally
            'objAdd.Dispose()
            'objClient.Dispose()
        End Try

    End Function


    Private Sub EditClient()
        Dim Logmessage As New System.Text.StringBuilder
        Dim isAlreadyOpen As Boolean = False
        Try
            If grdClients.ActiveRow Is Nothing Then Exit Sub
            Dim formText As String = "Crystal - Edit " + grdClients.ActiveRow.Cells("Client Name").Value
            For lmFomrs As Integer = 0 To System.Windows.Forms.Application.OpenForms.Count - 1
                If System.Windows.Forms.Application.OpenForms(lmFomrs).Name = "frmAddEditClient" Then
                    If CType(System.Windows.Forms.Application.OpenForms(lmFomrs), frmAddEditClient).WindowState = FormWindowState.Minimized Then
                        CType(System.Windows.Forms.Application.OpenForms(lmFomrs), frmAddEditClient).WindowState = FormWindowState.Normal
                    Else
                        System.Windows.Forms.Application.OpenForms(lmFomrs).BringToFront()
                    End If
                    FocusOnExistingInstance(formText, "frmAddEditClient", CType(System.Windows.Forms.Application.OpenForms(lmFomrs), frmAddEditClient).WindowState, isAlreadyOpen)
                    If isAlreadyOpen Then
                        Return
                    End If
                End If
            Next
            Dim frmEditClient As New frmAddEditClient
            frmEditClient.clientID = grdClients.ActiveRow.Cells("ClientID").Value
            frmEditClient.Text = formText
            frmEditClient.StartPosition = FormStartPosition.CenterScreen
            AddHandler frmEditClient.AddParticularClientInGrid, AddressOf AddParticularClientInGrid
            frmEditClient.Show()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            isAlreadyOpen = False
        End Try

    End Sub

    Private Function CreateDatatable() As System.Data.DataTable
        Dim Logmessage As New System.Text.StringBuilder

        Dim dt As System.Data.DataTable = Nothing
        Dim dr As DataRow = Nothing
        Try
            dt = New System.Data.DataTable
            dt.Columns.Add("ClientID", GetType(System.String))
            dt.Columns.Add("Client Name", GetType(System.String))
            dt.Columns.Add("Listing", GetType(System.String))
            dt.Columns.Add("Ratio", GetType(System.String))
            dt.Columns.Add("ISIN", GetType(System.String))
            dt.Columns.Add("SEDOL", GetType(System.String))
            dt.Columns.Add("CUSIP", GetType(System.String))
            dt.Columns.Add("Sector", GetType(System.String))
            dt.Columns.Add("Industry", GetType(System.String))
            dt.Columns.Add("Market Cap", GetType(System.String))
            dt.Columns.Add("Cap Group", GetType(System.String))
            dt.Columns.Add("Primary Exchange", GetType(System.String))
            dt.Columns.Add("Street1", GetType(System.String))
            dt.Columns.Add("Street2", GetType(System.String))
            dt.Columns.Add("City", GetType(System.String))
            dt.Columns.Add("State", GetType(System.String))
            dt.Columns.Add("Country", GetType(System.String))
            dt.Columns.Add("Region", GetType(System.String))
            dt.Columns.Add("Mobile", GetType(System.String))
            dt.Columns.Add("Telephone", GetType(System.String))
            dt.Columns.Add("Fax", GetType(System.String))
            dt.Columns.Add("Email", GetType(System.String))
            dt.Columns.Add("Notes", GetType(System.String))
            Return dt
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Return Nothing
        Finally
            dt = Nothing
            Logmessage = Nothing

        End Try
    End Function

    Public Sub AddParticularClientInGrid(ByVal ClientID As Integer)
        If (Me.IsDisposed) Then Exit Sub
        Dim dDtFund As New System.Data.DataTable
        Dim dDtgrdClients As System.Data.DataTable
        Dim Dr() As DataRow
        Dim dv As New DataView
        Dim dataSet As New DataSet
        Dim Drow As DataRow = Nothing
        Dim Logmessage As New System.Text.StringBuilder
        Try
            dataSet = ClientManagement.GetParticularClient(ClientID)
            dDtFund = dataSet.Tables(0)
            If dDtFund.Rows.Count = 0 Then Exit Sub
            dDtgrdClients = CType(grdClients.DataSource, System.Data.DataTable)
            If dDtgrdClients Is Nothing Then
                dDtgrdClients = CreateDatatable()
                For i As Integer = 0 To dDtFund.Rows.Count - 1
                    Drow = dDtgrdClients.NewRow
                    Drow("ClientID") = dDtFund.Rows(i)("ClientID")
                    Drow("Client Name") = dDtFund.Rows(i)("Client Name")
                    Drow("Listing") = dDtFund.Rows(i)("Listing")
                    Drow("Ratio") = dDtFund.Rows(i)("Ratio")
                    Drow("ISIN") = dDtFund.Rows(i)("ISIN")
                    Drow("SEDOL") = dDtFund.Rows(i)("SEDOL")
                    Drow("CUSIP") = dDtFund.Rows(i)("CUSIP")
                    Drow("Sector") = dDtFund.Rows(i)("Sector")
                    Drow("Industry") = dDtFund.Rows(i)("Industry")
                    Drow("Market Cap") = dDtFund.Rows(i)("Market Cap")
                    Drow("Cap Group") = dDtFund.Rows(i)("Cap Group")
                    Drow("Primary Exchange") = dDtFund.Rows(i)("Primary Exchange")
                    Drow("Street1") = dDtFund.Rows(i)("Street1")
                    Drow("Street2") = dDtFund.Rows(i)("Street2")
                    Drow("City") = dDtFund.Rows(i)("City")
                    Drow("State") = dDtFund.Rows(i)("State")
                    Drow("Country") = dDtFund.Rows(i)("Country")
                    Drow("Region") = dDtFund.Rows(i)("Region")
                    Drow("Mobile") = dDtFund.Rows(i)("Mobile")
                    Drow("Telephone") = dDtFund.Rows(i)("Telephone")
                    Drow("Fax") = dDtFund.Rows(i)("Fax")
                    Drow("Email") = dDtFund.Rows(i)("Email")
                    Drow("Notes") = dDtFund.Rows(i)("Notes")
                    dDtgrdClients.Rows.Add(Drow)
                Next

                dv = New DataView(dDtgrdClients)
                dv.Sort = "[Client Name] asc"
                grdClients.DataSource = dDtgrdClients
                grdClients.DisplayLayout.Bands(0).Columns("Client Name").PerformAutoResize(grdClients.Rows.Count)
                HideDefaultColumn()
            Else
                Dr = dDtgrdClients.Select("ClientID=" + ClientID.ToString())
                For Each row As DataRow In Dr
                    dDtgrdClients.Rows.Remove(row)
                Next
                dDtgrdClients.Merge(dDtFund, True, MissingSchemaAction.Ignore)
                grdClients.DataSource = dDtgrdClients
                grdClients.DisplayLayout.Bands(0).Columns("Client Name").PerformAutoResize(grdClients.Rows.Count)
                If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
                    HideRows(True)
                End If
                Exit Sub
                'End If
                dv = New DataView(dDtgrdClients)
                dv.Sort = "[Client Name] asc"
                grdClients.DataSource = dv.ToTable()
                grdClients.DisplayLayout.Bands(0).Columns("Client Name").PerformAutoResize(grdClients.Rows.Count)
            End If
            ShowCheckedGridColumns()
            grdClients.DisplayLayout.Bands(0).Columns("Client Name").PerformAutoResize(grdClients.Rows.Count)
            If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
                HideRows(True)
            End If
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
                Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
                Logmessage.Append(Environment.NewLine)
                Logmessage.Append("Error Message - ")
                Logmessage.Append(ex.ToString())
                Me.LogErrorToDatabase(Logmessage.ToString)
            End If
        Finally
            If grdClients.Rows.Count > 0 Then
                DisableButtonEditDelete(True)
                Dim colRow As UltraGridRow = Me.grdClients.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("ClientID").Text.Equals(ClientID.ToString(), StringComparison.CurrentCultureIgnoreCase))
                grdClients.ActiveRow = colRow
            Else
                DisableButtonEditDelete(False)
            End If
            Logmessage = Nothing
            dDtFund = Nothing
            dDtgrdClients = Nothing
            Dr = Nothing
            Dim str As String = GetRowCount(CType(grdClients.DataSource, System.Data.DataTable)).ToString
            lblTotalClients.Text = "Total Clients:- " + str

        End Try
    End Sub

    Private Sub CheckSelectedField(ByVal text As String)
        For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
            If DirectCast(ctrl, UltraCheckEditor).Text = text Then
                DirectCast(ctrl, UltraCheckEditor).Checked = True
            End If

        Next
    End Sub

#End Region

#Region "Grid Events"

    Private Sub grdClients_AfterSelectChange(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs) Handles grdClients.AfterSelectChange
        If grdClients.Selected.Rows.Count > 1 Then
            MultiRowSelection(False)
        Else
            MultiRowSelection(True)
        End If
    End Sub



    Private Sub grdClients_BeforeEnterEditMode(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdClients.BeforeEnterEditMode
        grdClients.ActiveRow.Activation = UltraWinGrid.Activation.ActivateOnly
    End Sub

#End Region

#Region "ToolTip"

    Private Sub GetTooltip(ByVal control As Control, ByVal tooltip As String)
        ToolTip2.SetToolTip(control, ToolTipManager.GetToolTip(tooltip))
    End Sub

    Private Sub btnExeSummary_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExeSummary.MouseHover
        GetTooltip(btnExeSummary, "EXESUMMARY")
    End Sub



    Private Sub btnAddClient_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddClient.MouseHover
        GetTooltip(btnAddClient, "ADDCLIENT")
    End Sub

    Private Sub btnExport_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.MouseHover
        GetTooltip(btnExport, "EXPORTTOEXCEL")
    End Sub

#End Region

#Region "CheckBox"

    Private Sub CheckBoxChangeSearchField()
        _SearchField = cmbSearchField.Text
        If txtSearchIn.Text.Trim <> MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Or txtSearchIn.Text.Trim <> MessageManager.GetMessage("DEFAULTDATEFORMAT").Trim Then
            _SearchValue = txtSearchIn.Text
        End If
        cmbSearchField.DataSource = LoadSearchField()
        cmbSearchField.Text = _SearchField
        If _SearchValue.Length > 0 Then
            txtSearchIn.Text = _SearchValue
        End If
    End Sub

    Private Sub ShowColumnOnChecked(ByVal uce As UltraCheckEditor)
        If grdClients.Rows.Count > 0 Then
            If Not grdClients.DisplayLayout.Bands(0).Columns.Exists(uce.Text) Then Exit Sub
            If uce.Checked Then
                grdClients.DisplayLayout.Bands(0).Columns(uce.Text).Hidden = False
            Else
                grdClients.DisplayLayout.Bands(0).Columns(uce.Text).Hidden = True
            End If
        End If
    End Sub

    Private Sub chkCapGroup_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCapGroup.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkCapGroup)
    End Sub

    Private Sub chkCity_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCity.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkCity)
    End Sub

    Private Sub chkCountry_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCountry.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkCountry)
    End Sub

    Private Sub chkCusip_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCusip.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkCusip)
        If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
            HideRows(True)
        Else
            HideRows(False)
        End If
    End Sub

    Private Sub chkEmail_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkEmail.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkEmail)
    End Sub

    Private Sub chkFax_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkFax.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkFax)
    End Sub

    Private Sub chkIndustry_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkIndustry.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkIndustry)
    End Sub

    Private Sub chkIsin_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkIsin.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkIsin)
        If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
            HideRows(True)
        Else
            HideRows(False)
        End If
    End Sub

    Private Sub chkListing_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkListing.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkListing)
        If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
            HideRows(True)
        Else
            HideRows(False)
        End If
    End Sub

    Private Sub chkMarketCap_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkMarketCap.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkMarketCap)
    End Sub

    Private Sub chkMobile_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkMobile.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkMobile)
    End Sub

    Private Sub chkNotes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkNotes.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkNotes)
    End Sub

    Private Sub chkPExchange_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPExchange.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkPExchange)
    End Sub

    Private Sub chkRatio_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRatio.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkRatio)
        If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
            HideRows(True)
        Else
            HideRows(False)
        End If
    End Sub

    Private Sub chkRegion_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRegion.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkRegion)
    End Sub

    Private Sub chkSector_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSector.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkSector)
    End Sub

    Private Sub chkSedol_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSedol.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkSedol)
        If Not chkListing.Checked And Not chkRatio.Checked And Not chkIsin.Checked And Not chkCusip.Checked And Not chkSedol.Checked Then
            HideRows(True)
        Else
            HideRows(False)
        End If
    End Sub

    Private Sub chkState_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkState.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkState)
    End Sub

    Private Sub chkStreet1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkStreet1.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkStreet1)
    End Sub

    Private Sub chkStreet2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkStreet2.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkStreet2)
    End Sub

    Private Sub chkTelephone_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTelephone.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkTelephone)
    End Sub
#End Region

#Region "Events"

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If grdClients.Rows.Count = 0 Then
            Exit Sub
        End If
        If (grdClients.ActiveRow Is Nothing) Then
            ShowMessageInfo(MessageManager.GetMessage("SELECTTHERECORD"))
            Exit Sub
        Else
            grdClients.Selected.Rows.Add(grdClients.ActiveRow)
        End If

        If grdClients.Selected.Rows().Count = 1 Then
            EditClient()
        Else
            ShowMessageInfo(MessageManager.GetMessage("SELECTROW"))
        End If
    End Sub
    Private Sub grdClients_BeforeSortChange(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeSortChangeEventArgs) Handles grdClients.BeforeSortChange
        If _IsSortColumn Then
            e.Cancel = True
            _IsSortColumn = False
        End If
    End Sub

    Private Sub grdClients_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdClients.Click

    End Sub

    Private Sub grdClients_DoubleClickRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs) Handles grdClients.DoubleClickRow
        EditClient()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If grdClients.Rows.Count = 0 Then
            Exit Sub
        End If
        If (grdClients.ActiveRow Is Nothing) Then
            ShowMessageInfo(MessageManager.GetMessage("SELECTTHERECORD"))
            Exit Sub
        Else
            grdClients.Selected.Rows.Add(grdClients.ActiveRow)
        End If
        If DeleteClient.CheckClientDeleted(grdClients.ActiveRow.Cells("ClientID").Value) Then
            If (MessageBox.Show(MessageManager.GetMessage("WANT_DELETE_CLIENT").Trim, BaseClass.crystalCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = System.Windows.Forms.DialogResult.Cancel) Then
                Exit Sub
            End If
        End If
        If Not DeleteClient.IsClientDeleted(grdClients.ActiveRow.Cells("ClientID").Value) Then
            Dim deleteClient As New frmDeleteClient
            deleteClient.clientID = grdClients.ActiveRow.Cells("ClientID").Value
            deleteClient.ShowDialog()
            If deleteClient.ClientDeleted = True Then
                Dim dDtgrdClients As System.Data.DataTable
                Dim Dr() As DataRow
                dDtgrdClients = CType(grdClients.DataSource, System.Data.DataTable)
                Dr = dDtgrdClients.Select("ClientID=" + deleteClient.clientID.ToString())
                For Each row As DataRow In Dr
                    dDtgrdClients.Rows.Remove(row)
                Next
            End If
        Else
            Dim dDtgrdClients As System.Data.DataTable
            Dim Dr() As DataRow
            dDtgrdClients = CType(grdClients.DataSource, System.Data.DataTable)
            Dr = dDtgrdClients.Select("ClientID=" + grdClients.ActiveRow.Cells("ClientID").Value.ToString())
            For Each row As DataRow In Dr
                dDtgrdClients.Rows.Remove(row)
            Next
            MessageBox.Show(MessageManager.GetMessage("CLIENT_DELETED"), BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    Private Sub txtSearchIn_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchIn.Enter
        If txtSearchIn.Text.Trim = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Then
            txtSearchIn.Text = String.Empty
        End If
    End Sub

    Private Sub txtSearchIn_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearchIn.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtSearchIn.Text = String.Empty Then
                _AllSearch = True
            End If
            LoadClients()
            If txtSearchIn.Text.Length > 0 Then
                rdSearchResult.Checked = True
            End If
        End If
    End Sub

    Private Sub cmbSearchField_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSearchField.SelectionChanged
        If cmbSearchField.Value = _NullField Then
            ShowMessageInfo(MessageManager.GetMessage("INVALIDSEARCHFIELD").Trim)
            cmbSearchField.Text = _ClientName
            Return
        Else
            CheckSelectedField(cmbSearchField.Text)
        End If
    End Sub

    Private Sub grdClients_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdClients.InitializeLayout
        'e.Layout.Override.MergedCellContentArea = MergedCellContentArea.VirtualRect
        'e.Layout.Bands(0).Columns("Client Name").MergedCellStyle = MergedCellStyle.Default
        e.Layout.Override.MergedCellStyle = MergedCellStyle.Always

        ' MergedCellEvaluator property can be used to speficy custom logic for
        ' merging cells.
        For i As Integer = 1 To e.Layout.Bands(0).Columns.Count - 1
            e.Layout.Bands(0).Columns(i).MergedCellEvaluator = New CustomMergedCellEvaluator()
        Next

    End Sub

#End Region


    Private Sub grdClients_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdClients.MouseDown
        Dim mouseupUIElement As UIElement _
            = grdClients.DisplayLayout.UIElement.ElementFromPoint( _
            New System.Drawing.Point(e.X, e.Y))

        ' retrieve the Column from the UIElement
        Dim mouseupColumn As UltraGridColumn _
        = mouseupUIElement.GetContext(GetType(UltraGridColumn))

        ' if there is a column object reference, display column key
        If Not mouseupColumn Is Nothing Then
            If mouseupColumn.Key = "ISIN" Or mouseupColumn.Key = "Listing" Or _
                mouseupColumn.Key = "Ratio" Or mouseupColumn.Key = "CUSIP" Or mouseupColumn.Key = "SEDOL" Then
                _IsSortColumn = True
            End If
        End If
        Dim index As Integer = -1
        'If _IsSortColumn Then
        '    Dim rows() As UltraWinGrid.UltraGridRow
        '    Dim clientID As String = ""
        '    For i As Integer = 0 To grdClients.Rows.Count - 1
        '        If grdClients.Rows(i).Cells(0).Value.ToString <> grdClients.Rows(i + 1).Cells(0).Value.ToString Then
        '            clientID = grdClients.Rows(i).Cells(0).Value.ToString
        '            ' rows = grdClients.Rows.SelectMany(Of UltraGridRow).t.ToArray(Function(row) row.Cells(0).Text.Equals(clientID.ToString, StringComparison.CurrentCultureIgnoreCase))
        '        End If
        '    Next
        'End If
    End Sub
End Class
Class CustomMergedCellEvaluator
    Implements Infragistics.Win.UltraWinGrid.IMergedCellEvaluator

    Function ShouldCellsBeMerged(ByVal row1 As UltraGridRow, ByVal row2 As UltraGridRow, ByVal column As UltraGridColumn) As Boolean Implements IMergedCellEvaluator.ShouldCellsBeMerged
        Dim name1 As String = DirectCast(row1.GetCellValue(column).ToString, String)
        Dim name2 As String = DirectCast(row2.GetCellValue(column).ToString, String)
        If column.Key = "Client Name" Then
            If name1 = name2 Then
                If row1.Index = 0 Then
                    row1.Appearance.BackColor = Color.White
                    row2.Appearance.BackColor = Color.White
                Else
                    If row1.Appearance.BackColor = Color.White Then
                        row2.Appearance.BackColor = Color.White
                    Else
                        row2.Appearance.BackColor = Color.FromArgb(241, 245, 250)
                    End If
                End If
            Else
                If row1.Appearance.BackColor = Color.White Then
                    row2.Appearance.BackColor = Color.FromArgb(241, 245, 250)
                Else
                    row2.Appearance.BackColor = Color.White
                End If
            End If
            'If name1 = name2 Then
            '    row1.Appearance.BackColor = Color.White
            'End If
            Return name1 = name2
        ElseIf column.Key = "Listing" Then
            Return False
        ElseIf column.Key = "" Then
            Return False
        ElseIf column.Key = "Ratio" Then
            Return False
        ElseIf column.Key = "ISIN" Then
            Return False
        ElseIf column.Key = "CUSIP" Then
            Return False
        ElseIf column.Key = "SEDOL" Then
            Return False
        ElseIf column.Key = "Sector" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Industry" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return (name1 = name2)
        ElseIf column.Key = "Market Cap" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Cap Group" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Primary Exchange" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Street1" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Street2" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "City" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "State" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Country" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Region" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Mobile" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Telephone" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Fax" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Email" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "Notes" Then
            If row2.Cells(1).Value.ToString <> row1.Cells(1).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        End If
        'If col

        ' Merge cells according to the date portions of the underlying DateTime cell
        ' values, ignoring any time portion. For example, "1/1/2004 10:30 AM" will be
        ' merged with "1/1/2004 1:15 AM" since the dates are the same.
        Return False
    End Function
End Class

